/*
 * =====================================================================================
 *
 *       Filename:  SocketMgrLinux.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012-10-01
 *       Revision:  none
 *       Compiler:  cmake & gcc
 *
 *         Author:  ankjt
 *
 * =====================================================================================
 */

#ifndef __SOCKETMGR_LINUX_H__
#define __SOCKETMGR_LINUX_H__

#include "SocketDefines.h"
#include "Thread.h"
#include "Socket.h"
#include "TeNetMgr.h"


#define SOCKET_HOLDER_SIZE 30000   
#define THREAD_EVENT_SIZE 4096     

class SocketWorkerThread;
class ListenSocketBase;

class SocketMgr : public Thread
{
 public:
    friend class SocketWorkerThread;

    SocketMgr();
    ~SocketMgr();

    bool Init();
    void Update();
    void SetNetCall(TeNetCall* pkCall){ m_pkCall = pkCall; }
    TeNetCall* GetCall(){ return m_pkCall;}
    
    Socket* CreateSocket(SOCKET sSocket,int iSendBufferSize,int iRecvBufferSize);
    void AddSocket(Socket* s);
    void RemoveSocket(Socket* s);
    int  GetSocketCount();
    
    Socket* ConnectTCPSocket(const char * pcHostName,int iPort,int iSendBufferSize,int iRecvBufferSize);
    inline int GetEpollFd() { return m_iEpollFd; }

    bool SendBuffer(SOCKET sFd,void* pData,int iSize);

    void PushSocket(Socket* s);

    void DisConnect(SOCKET sFd);
    void DisConnect(Socket* s);
    void CloseAll();

    void SpawnWorkerThreads();
    void Run();

private:
    int m_iEpollFd;
    struct epoll_event m_kEvents[THREAD_EVENT_SIZE];

    TeNetCall* m_pkCall;
    std::map<SOCKET,Socket*> m_kFdMap;
    Mutex                    m_kFdMutex;
};

#endif
